package com.jason.kotlin.gank.ui

import android.text.TextUtils
import android.widget.Button
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.model.RollCommonBean
import com.jason.kotlin.gank.ui.adapter.RollCommonQueryAdapter
import com.jason.kotlin.gank.viewmodel.RollMoreViewModel
import com.jason.library.common.base.BaseStateActivity
import com.jason.library.common.ext.observe
import com.jason.library.common.util.KeyboardUtils
import com.jason.library.common.util.StringMatherUtils
import com.jason.library.widget.edittext.MClearEditText
import com.jason.library.widget.snackbar.MySnackbar

/**
 * @description: RollPhoneAddressActivity
 *
 * @author ChenBaoyang
 * @date 4/23/21 14:32
 */
class RollPhoneAddressActivity : BaseStateActivity<RollMoreViewModel>() {

    private lateinit var queryBtn: Button
    private lateinit var phoneEdit: MClearEditText
    private lateinit var recyclerView: RecyclerView

    private lateinit var queryAdapter: RollCommonQueryAdapter

    override fun bindLiveData() {
        observe(viewModel!!.dataPhone) {
            val datas = mutableListOf<RollCommonBean>()
            datas.add(RollCommonBean("手机号码:", it.mobile))
            datas.add(RollCommonBean("归属地省份:", it.province))
            datas.add(RollCommonBean("归属地描述:", it.carrier))
            queryAdapter.setNewData(datas)
        }
    }

    override fun getPageTarget() = recyclerView

    override fun reload() {

    }

    override fun getLayoutId() = R.layout.roll_activity_phone_address

    override fun initView() {
        title = "手机号码归属地查询"
        queryBtn = findViewById(R.id.btn_query)
        phoneEdit = findViewById(R.id.edit_text_phone)
        recyclerView = findViewById(R.id.recyclerView)

        queryAdapter = RollCommonQueryAdapter(R.layout.roll_item_common_query)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = queryAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()

        queryBtn.setOnClickListener {
            KeyboardUtils.hideInputKeyboard(this)

            //获取手机号码
            val phoneNumber: String = phoneEdit.getText().toString()

            if (TextUtils.isEmpty(phoneNumber)) {
                MySnackbar.makeSnackBarRed(toolbar, "手机号码不能为空")
                return@setOnClickListener
            }

            if (!StringMatherUtils.isPhoneLegal(phoneNumber)) {
                MySnackbar.makeSnackBarRed(toolbar, "手机号码格式不正确")
                return@setOnClickListener
            }
            viewModel!!.getMobileLocation(phoneNumber)
        }
    }

    override fun onDataInit() {

    }
}